class Counter{
    private int count=0;
    synchronized public void add(){
        count++;
    }
    public int get(){
        return count;
    }
    public synchronized static void func(){
    //获取类对象里面的锁
        synchronized (Counter.class){//确保了在同一时刻，只有一个线程可以执行这个代码块
            // 这里的代码块将被类级别的锁保护
        }
    }
}
public class ThreadDemo18MothodLook {
    public static void main(String[] args) throws InterruptedException {
        StringBuilder stringBuilder;
        Object locker=new Object();
        Counter counter=new Counter();
        Thread t1=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.add();
            }
        });
        Thread t2=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.add();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count="+counter.get());
    }
}
